#!/bin/bash
# JDK包来源 https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
# 例: sh install-OracleJDK.sh

filename="jdk-8u202-linux-x64.tar.gz"
file_md5="0029351f7a946f6c05b582100c7d45b7"
env_profile_path=${HOME}/.bash_profile

if [[ ! -e "${filename}" ]];then
    curl -sSO "https://repo.huaweicloud.com/java/jdk/8u202-b08/${filename}"
fi

echo "${file_md5} ${filename}" 2>&1 | md5sum -c
if [[ $? != 0 ]];then
    echo "下载JDK包 https://repo.huaweicloud.com/java/jdk/8u202-b08/${filename}"
    curl -sSO "https://repo.huaweicloud.com/java/jdk/8u202-b08/${filename}"
fi

java_home=${HOME}/$(tar -tf "${filename}" | head -1 | awk -F / '{print $1}')

tar -zxf ${filename} -C ${HOME}/

sed -r -e "/(JAVA_HOME|JDK|java_home|jdk)/d" -i ${env_profile_path}

echo "
# OracleJDK
export JAVA_HOME=${java_home}
export JRE_HOME=\${JAVA_HOME}/jre
export CLASSPATH=.:\${JAVA_HOME}/lib/dt.jar:\${JAVA_HOME}/lib/tools.jar
export PATH=\${JAVA_HOME}/bin:\$PATH
" >> ${env_profile_path}

grep -iP "java|jdk" "${env_profile_path}"

source ${env_profile_path}

java -version